package com.zyj.eps.controller;

import com.alibaba.fastjson.JSON;
import com.zyj.eps.pojo.User;
import com.zyj.eps.pojo.Utils;
import com.zyj.eps.pojo.Vaccine;
import com.zyj.eps.pojo.Vapply;
import com.zyj.eps.service.UserServiceImpl;
import com.zyj.eps.service.VaccineService;
import com.zyj.eps.service.VaccineServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author super
 * @date 2022/3/30 15:43
 */
@Controller
@RestController
@CrossOrigin
@SuppressWarnings("all")//抑制警告
public class VaccineController {
    @Autowired
    private VaccineServiceImpl vaccineService;
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getAllVaccine")
    public String getAllVaccine() {
        HashMap<String, Object> res = new HashMap<>();
        List<Vaccine> list = vaccineService.getAllVaccine();
        System.out.println(list);
        List Vlist = new ArrayList();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        for (Vaccine vaccine : list) {
            /*比较当调用此方法的Date对象和指定日期。两者相等时候返回0。
             调用对象在指定日期之前则返回负数。调用对象在指定日期之后则返回正数。*/
            int i = date.compareTo(vaccine.getVtime());
            if (i < 0) {
                Vlist.add(vaccine);
            }//当时间在当前时间之后,就将此疫苗记入Vlist中
        }
        System.out.println(Vlist);
        res.put("VaccineList", Vlist);
        return JSON.toJSONString(res);
    }

    @CrossOrigin
    @RequestMapping("/ApplyVaccine")
    public String ApplyVaccine(@RequestBody Utils utils) {
        User user = userService.getUserByUUID(utils.getUserID());
        Vaccine vaccine = vaccineService.getVaccineByVid(utils.getVid());
        /*System.out.println(utils);
        System.out.println(vaccine);
        System.out.println(user);*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        vaccineService.InsertVaccineApply(user.getUname(), user.getUid(), vaccine.getVid(), vaccine.getVname(), sdf.format(new Date()), vaccine.getVtime(), vaccine.getVaddress());
        return JSON.toJSONString("ok");
    }

    @RequestMapping("/getApplyVaccineList")
    public String getApplyVaccineList(int vuid) {
        List<Vapply> applyVaccineList = vaccineService.getApplyVaccineList(vuid);
        Collections.reverse(applyVaccineList); //实现list集合逆序排列
        HashMap<String, Object> map = new HashMap<>();
        map.put("getApplyVaccineByUid", applyVaccineList);
        return JSON.toJSONString(map);
    }
}
